package codetop.microsoft.T39;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 18362
 * @Create: 2022-08-14 10:10:45 星期日
 */
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> results = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        dfs(results, temp, candidates, target, 0);
        return results;
    }

    private void dfs(List<List<Integer>> results, List<Integer> temp, int[] candidates, int target, int cur) {
        if (cur == candidates.length) {
            if (target == 0)
                results.add(new ArrayList<>(temp));
            return;
        }
        if (target < 0)
            return;
        dfs(results, temp, candidates, target, cur+1);
        temp.add(candidates[cur]);
        dfs(results, temp, candidates, target - candidates[cur], cur);
        temp.remove(temp.size()-1);
    }
}